package com.mchsdk.paysdk.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mchsdk.paysdk.bean.LoginModel;
import com.mchsdk.paysdk.bean.PersonalCenterModel;
import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.entity.UserLogin;
import com.mchsdk.paysdk.http.process.UpdateVisitorInfoProcess;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.utils.PreSharedManager;
import com.mchsdk.paysdk.utils.TextUtils;
import com.mchsdk.paysdk.utils.ToastUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhujinzhujin
 * on 2017/1/17.
 */

public class MCVisitorUpdateInfo extends MCBaseActivity {

    /**
     * 日志打印
     */
    private final static String TAG = "MCVisitorUpdateInfo";


    private EditText etVisitorName;
    private EditText etVisitorPwd;

    String userName;
    String pwd;
    /**
     * 设置应户名请求后的操作
     */
    private Handler visitorInfoHandle = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case Constant.UPDATE_VISITOR_INFO_SUCCESS:
                    handlerUpdateRes((String) msg.obj);
                    ToastUtil.show(MCVisitorUpdateInfo.this,"用户名设置成功");
                    break;
                case Constant.UPDATE_VISITOR_INFO_FAIL:
                    ToastUtil.show(MCVisitorUpdateInfo.this,msg.obj.toString());
                    MCLog.e(TAG, "error:" + msg.obj.toString());
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayout("activity_mch_personal_info_setuser"));

        initTitle();

        userName = "";
        pwd = "";
    }

    private void initTitle(){
        TextView txtTitle = (TextView) findViewById(getId("tv_mch_header_title"));
        txtTitle.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
        txtTitle.setText("设置用户名");
        ImageView ivBack = (ImageView) findViewById(getId("iv_mch_header_back"));
        ivBack.setVisibility(View.VISIBLE);
//        ImageView ivClose = (ImageView) findViewById(getId("iv_mch_header_close"));
//        ivClose.setVisibility(View.GONE);
//        ivClose.setOnClickListener(backClick);
        ivBack.setOnClickListener(backClick);

        etVisitorName = (EditText) findViewById(getId("et_mch_visitor_username"));
        etVisitorPwd = (EditText) findViewById(getId("et_mch_visitor_pwd"));

        Button btnUpdateVisitorInfo = (Button) findViewById(getId("btn_mch_update_visitorinfo"));
        btnUpdateVisitorInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitUpdateInfo();
            }
        });
    }

    View.OnClickListener backClick = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    };

    /**
     * 启动设置用户名请求,并配置需要的参数
     */
    private void submitUpdateInfo(){
        userName = etVisitorName.getText().toString().trim();
        pwd = etVisitorPwd.getText().toString().trim();
        if(TextUtils.isEmpty(userName) || TextUtils.isEmpty(pwd)){
            MCLog.w(TAG, "userName:" + userName + ", pwd:" + pwd);
            ToastUtil.show(MCVisitorUpdateInfo.this,"用户名或密码不能为空");
            return;
        }

        //判断输入的用户名及密码是否为6-15位字符
        String regular=Constant.REGULAR_ACCOUNT;
        Pattern pattern=Pattern.compile(regular);
        Matcher matcher_pwd = pattern.matcher(pwd);
        Matcher matcher_userName = pattern.matcher(userName);
        boolean b_pwd = matcher_pwd.matches();
        boolean b_userName = matcher_userName.matches();
        if(!b_pwd||!b_userName)
        {
            ToastUtil.show(MCVisitorUpdateInfo.this,"用户名或密码应为6-15位数字或英文字母");
            return;
        }
        UpdateVisitorInfoProcess updateVisitorInfo = new UpdateVisitorInfoProcess();
        updateVisitorInfo.setAccount(userName);
        updateVisitorInfo.setPwd(pwd);
        updateVisitorInfo.post(visitorInfoHandle);

    }

    /**
     * 请求成功后保存
     * @param res
     */
    private void handlerUpdateRes(String res){
        MCLog.w(TAG, "Update res:" + res);
        UserLogin userLogin = new UserLogin();
        userLogin.setUserName(userName);
        userLogin.setPassword(pwd);
        userLogin.setAccountUserId(PersonalCenterModel.getInstance().getUserId());
        userLogin.setYKLogin(false);
//        LoginModel.instance().loginSuccess(true, true, userLogin);
        LoginModel.instance().saveUserInfoToPre(true, true, userLogin);
        PreSharedManager.setString(Constant.CUSTOMER_YK, "", MCVisitorUpdateInfo.this);
        finish();
    }
}
